/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgetgriddemo.server.handlers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;

import finbudget.client.ui.table.Record;
import finbudgetgriddemo.client.grid.GetDataAction;
import finbudgetgriddemo.client.grid.GetDataResult;
import finbudgetgriddemo.client.grid.GridView;
import finbudgetgriddemo.server.Database;
import finbudgetgriddemo.server.ServerUtils;

public class GetDataHandler implements ActionHandler<GetDataAction, GetDataResult> {
	private static final Random random = new Random();

	@Override
	public GetDataResult execute(GetDataAction action, ExecutionContext context) throws ActionException {
		// Keep in mind request timeout (set 58 seconds)
		long timeout = new Date().getTime() + 58L * 1000L;
		if (action == null) {
			throw new ActionException("GetDataHandler error: null argument is not permitted");
		}
		if (action.getRecordsToGet() <= 0) {
			throw new ActionException("GetDataHandler error: the number of records requested must be greater then zero. Provided in request: " + action.getRecordsToGet());
		}
		List<Record> result = new ArrayList<Record>(action.getRecordsToGet());
		for (int i = 0; i < action.getRecordsToGet(); i++) {
			result.add(generateRandomRecord(i));
			if (new Date().getTime() >= timeout) {
				break;
			}
		}
		return new GetDataResult(result);
	}

	@Override
	public Class<GetDataAction> getActionType() {
		return GetDataAction.class;
	}

	@Override
	public void undo(GetDataAction arg0, GetDataResult arg1,ExecutionContext arg2) throws ActionException {
		throw new ActionException("GetDataHandler.undo not implemented");		
	}

	private Record generateRandomRecord(int key) {
		Record record = new Record("" + key);
		record.setValue(GridView.fieldName, 
				Database.FIRST_NAMES[random.nextInt(Database.FIRST_NAMES.length)] + " " + Database.LAST_NAMES[random.nextInt(Database.LAST_NAMES.length)]);
		record.setValue(GridView.fieldWage, Integer.toString(1000 + random.nextInt(2000)));
		record.setValue(GridView.fieldAge, Integer.toString(18 + random.nextInt(50)));
		record.setValue(GridView.fieldDate, ServerUtils.dateToString(new Date(new Date().getTime() - 1000L * 60L * 60L * 24L * new Long(random.nextInt(365*10)))));
		record.setValue(GridView.fieldTime, Long.toString(new Date().getTime() - 1000L * 60L * 60L * 24L * new Long(random.nextInt(365*10))));
		return record;
	}
}
